/*
 * Copyright © 2009 Perseus Project - Tufts University <http://www.perseus.tufts.edu>
 *
 * This file is part of UniCollatorPerseus.
 *
 * OcroPerseus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * OcroPerseus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with OcroPerseus.  If not, see <http://www.gnu.org/licenses/>.
 */

package eu.himeros.text;

import eu.himeros.util.IoManager;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.util.Hashtable;
import java.util.Stack;
import java.util.Vector;

/**
 * Finds words containing a given char. Useful for rare chars (e.g. epsilon with dasia and grave)
 *
 * @author Federico Boschetti <federico.boschetti.73@gmail.com>
 */
public class FindWordsWithChar {
    BufferedReader wordBr=null;
    BufferedReader charBr=null;
    BufferedWriter bw=null;
    Vector<String> wordsV=new Vector<String>(100000);
    Vector<String> charsV=new Vector<String>(500);
    Hashtable<String,Integer> charsHt=new Hashtable<String, Integer>(500);
    Stack<Integer> removendaStk=new Stack<Integer>();

    /**
     * Default constructor
     */
    public FindWordsWithChar(){}

    /**
     * Constructor with args.
     * @param args
     */
    public FindWordsWithChar(String[] args){
        init(args);
    }

    /**
     * Init the class.
     * @param args
     */
    protected void init(String[] args){
        String line=null;
        String[] items;
        int cnt=0;
        String charX=null;
        try{
            wordBr=IoManager.getReader(args[0]);
            charBr=IoManager.getReader(args[1]);
            bw=IoManager.getWriter(args[2]);
            while((line=charBr.readLine())!=null){
                charsV.add(line.substring(0,line.indexOf(' ')));
                charsHt.put(line.substring(0,line.indexOf(' ')),new Integer(args[3]));
            }
            lb0:
            while((line=wordBr.readLine())!=null){
                items=line.split(" ");
                if(items==null){items=new String[1];items[0]=line;}
                lb1:
                for(int i=0;i<items.length;i++){
                    for(int j=0;j<charsV.size();j++){
                        charX=charsV.elementAt(j);
                        if(items[i].contains(charX)){
                            cnt=charsHt.get(charX).intValue();
                            //System.out.println(charX+": "+cnt+" "+items[i]);
                            bw.write(charX+" "+items[i]);bw.newLine();
                            cnt--;
                            charsHt.put(charsV.elementAt(j),cnt);
                            if(cnt==0){
                                removendaStk.push(new Integer(j));
                            }                            
                        }
                    }
                    while(!removendaStk.empty()){
                        charsV.remove(removendaStk.pop().intValue());
                        if(charsV.size()==0) break lb0;
                    }
                }
            }
            for(String itm:charsV){
                if(charsHt.get(itm).intValue()==5){
                    bw.write(itm);bw.newLine();
                }
            }
            bw.close();
        }catch(Exception ex){ex.printStackTrace();}
    }

    /**
     * The <code>main</code> method.
     * @param args
     */
    public static void main(String[] args){
        if(args==null||args.length==0){
            System.out.println("FindWordsWithChar is used to find words with rare chars");
            System.out.println("Usage: java eu.himeros.ocrtrainer.FindWordsWithChar <inFile> <charFile> <outFile> <first_n_occurrences>");
            System.out.println("<inFile> is the text to search;");
            System.out.println("<charFile> is a list of chars, in the format: char<tab>numberOfExpectedOccurrences (it could be 0)");
            System.out.println("<outFile> contains the list of retrieved words");
            System.out.println("<first_n_occurrences> set the number of first occurrences with a char listed in <charFile>");
            System.exit(1);
        }
        FindWordsWithChar fwwc=new FindWordsWithChar(args);
    }

}
